﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Kaidou.Persister;
using Kaidou.Model;
using Kaidou.Util;

namespace Kaidou.Util
{
    public class StringUtil
    {
        public static readonly string STATUS_NORMAL = "10";
        public static readonly string STATUS_DELETE = "99";
        public static readonly string USER_CLSS_KUMIAIIN = "10";
        public static readonly string USER_CLSS_JIMUKYOKU = "20";
        public static readonly string USER_CLSS_OPERATOR = "30";
        public static readonly string CANCEL_REST_FLG_NORMAL = "0";
        public static readonly string CANCEL_REST_FLG_FORCE = "1";
        public static readonly string PASS_CHANGE = "1";
        private static readonly string checkmachine_name =
        "アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲンァィゥェォッャュョヴガギグゲゴザジズゼゾダヂヅデドバビブベボパピプペポABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzＡＢＣＤＥＦＧＨＩＪＫＬＭＮＯＰＱＲＳＴＵＶＷＸＹＺａｂｃｄｅｆｇｈｉｊｋｌｍｎｏｐｑｒｓｔｕｖｗｘｙｚ0123456789０１２３４５６７８９-ー/／=＝()（）" +
        "〇○◇□△▽☆●◆■▲▼★◎◯♂♀〒（）〔〕［］｛｝〈〉《》「」『』【】‘’“”→←↑↓⇒⇔…‥、。，．・：；？！゛゜´｀¨＾ヽヾゝゞ〃°′″・･￥＄￠￡％‰℃Å=+-±×÷=≒≠≦≧<>≪≫∞∽∝∴∵∈∋⊆⊇⊂⊃∪∩∧∨￢⇒⇔∀∃∠⊥⌒∂∇≡√∫∬ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩαβγδεζηθικλμνξοπρστυφχψωАБВГДЕЁЖЗИЙКЛМНО" +
        "ПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюя＃＆＊＠§※〓♯♭♪†‡¶仝々〆ー～￣＿―‐∥｜／＼”’＄＃％！｜？＜＞，．／＠‘＊＋；：\"'$#%!|?<>,./@`*+;:";
        private static readonly string[] initial = { "T-", "F-", "O-", "H-", "K-", "P-", "X-", "M-" };

        private static readonly string hiragana = "あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもやゆよらりるれろわをんぁぃぅぇぉっゃゅょ";
        private static readonly string hiraganaDash = "がぎぐげござじずぜぞだぢづでどばびぶべぼぱぴぷぺぽ";
        private static readonly string katakana = "アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲンァィゥェォッャュョ";
        private static readonly string katakanaDash = "ヴガギグゲゴザジズゼゾダヂヅデドバビブベボパピプペポ";
        private static readonly string hankaku = "ｱｲｳｴｵｶｷｸｹｺｻｼｽｾｿﾀﾁﾂﾃﾄﾅﾆﾇﾈﾉﾊﾋﾌﾍﾎﾏﾐﾑﾒﾓﾔﾕﾖﾗﾘﾙﾚﾛﾜｦﾝｧｨｩｪｫｯｬｭｮ";
        private static readonly string[] hankakuDash = { "ｳﾞ", "ｶﾞ", "ｷﾞ", "ｸﾞ", "ｹﾞ", "ｺﾞ", "ｻﾞ", "ｼﾞ", "ｽﾞ", "ｾﾞ", "ｿﾞ", "ﾀﾞ", "ﾁﾞ", "ﾂﾞ", "ﾃﾞ", "ﾄﾞ", "ﾊﾞ", "ﾋﾞ", "ﾌﾞ", "ﾍﾞ", "ﾎﾞ", "ﾊﾟ", "ﾋﾟ", "ﾌﾟ", "ﾍﾟ", "ﾎﾟ" };
        private static readonly string zenAlpha = "ＡＢＣＤＥＦＧＨＩＪＫＬＭＮＯＰＱＲＳＴＵＶＷＸＹＺａｂｃｄｅｆｇｈｉｊｋｌｍｎｏｐｑｒｓｔｕｖｗｘｙｚ！”＃＄％＆’（）￣＝～｜－＾￥＠＋＊／＜＞．，？［］｛｝＿：；";
        private static readonly string zenAlphaNoKako = "ＡＢＣＤＥＦＧＨＩＪＫＬＭＮＯＰＱＲＳＴＵＶＷＸＹＺａｂｃｄｅｆｇｈｉｊｋｌｍｎｏｐｑｒｓｔｕｖｗｘｙｚ！”＃＄％＆’￣＝～｜－＾￥＠＋＊／＜＞．，？［］｛｝＿：；";
        private static readonly string hanAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!\"#$%&'()~=~|-^\\@+*/<>.,?[]{}_:;";
        private static readonly string hanNumber = "0123456789";
        private static readonly string zenSuuji = "０１２３４５６７８９";
        private static readonly string mailValid = "abcdefghijklmnopqrstuvwxyz._-0123456789";
        private static readonly string delim = "()-/_";
        private static readonly string hanAlpha_komozi = "abcdefghijklmnopqrstuvwxyz";
        private static readonly string passwordCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

        public static bool isAscii(String str)
        {
            if (str == null)
                return false;

            return isComposed(str, hanAlpha + hanNumber);
        }

        public static string getRandomPassword()
        {
            Random random = new Random();
            int length = random.Next(1, 4) + 6;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < length; i++)
            {
                int index = random.Next(1, passwordCharacters.Length - 1);
                sb.Append(passwordCharacters.ToCharArray()[index]);
            }
            return sb.ToString();
        }

        public static bool isZenkaku(string strMoji)
        {
            Encoding sjisEncoding = Encoding.GetEncoding("Shift_JIS");
            return (sjisEncoding.GetByteCount(strMoji) == strMoji.Length * 2);
        }

        public static bool isZipNo(string str)
        {
            for (int i = 0; i < str.Length; i++)
            {
                string achr = str.ToCharArray()[i].ToString();
                if (hanNumber.IndexOf(achr) == -1)
                    return false;
            }
            return true;
        }

        /// <summary>
        /// 指定文字列全てが半角文字列かどうかを判定します。
        /// </summary>
        /// <param name="strMoji ">判定対象文字列</param>
        /// <returns>true:半角文字列です。false:半角文字列ではありません（全半角混合、全角のみ）。</returns>
        public static bool IsHankaku(string strMoji)
        {
            Encoding sjisEncoding = Encoding.GetEncoding("Shift_JIS");
            return (sjisEncoding.GetByteCount(strMoji) == strMoji.Length);
        }

        public static bool containsHankakuKana(string text)
        {
            return isContained(text, (hankaku + "ﾞﾟ｡､ｰ"));
        }

        public static bool containsAlpha_komozi(string str)
        {
            return isContained(str, (hanAlpha_komozi + "ﾞﾟ｡､ｰ"));
        }

        public static bool containsInitial(string str)
        {
            for (int i = 0; i < initial.Length; i++)
                if (isEquals(str.Substring(0, 2), initial[i]))
                {
                    return false;
                }
            {
                return true;
            }
        }

        public static bool containsZenkakuAlpha(string str)
        {
            return isContained(str, (zenAlpha + "ﾞﾟ｡､ｰ"));
        }

        public static bool containsZenkakuSuuji(string str)
        {
            return isContained(str, (zenSuuji + "ﾞﾟ｡､ｰ"));
        }

        public static bool isKatakana(string str)
        {
            return isComposed(str, (katakana + katakanaDash + "ー。、"));
        }

        public static bool isContained(string str1, string str2)
        {
            string str = null;
            string chars = null;
            //文字数の少ないほうを調べる文字の集合として認識
            if (str1.Length >= str2.Length)
            {
                str = str1;
                chars = str2;
            }
            else
            {
                str = str2;
                chars = str1;
            }
            for (int i = 0; i < chars.Length; i++)
            {
                string achr = chars.ToCharArray()[i].ToString();
                if ((str.IndexOf(achr)) != -1)
                {
                    return true;
                }
            }
            return false;

        }

        public static bool isEquals(string str1, string str2)
        {
            try
            {
                if (str1.Equals(str2))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }

        public static bool isComposed(string str1, string str2)
        {
            if ((str1.Equals("")) || (str2.Equals("")))
            {
                return false;
            }

            for (int i = 0; i < str1.Length; i++)
            {
                string achr = str1.ToCharArray()[i].ToString();
                if ((str2.IndexOf(achr)) == -1)
                {
                    return false;
                }
            }
            return true;


        }

        /**
	     * 文字列が半角数字のみかどうか調べる．
	     *
	     * @param str
	     * @return 半角数字のみ場合はtrue．そうでなければfalse．
	     */
        public static bool isHankakuNumber(string str)
        {
            if (str == null)
                return false;

            return isComposed(str, hanNumber);
        }

        public static bool isEmpty(string text)
        {
            if (text == null) return true;
            return text.Trim().Length == 0;
        }

        public static bool isNumber(string number)
        {
            bool flag = true;
            Regex r = new Regex(@"^\d+$");
            if (!r.IsMatch(number))
            {
                flag = false;
            }

            return flag;
        }

        /**
        * 電話番号かどうかを判定する。
        * @param target 対象文字列
        * @return 電話番号：true / それ以外：false
        */
        public static bool isTelNo(String target)
        {
            if (target.StartsWith("090") || target.StartsWith("070") || target.StartsWith("080"))
            {
                if (target.Trim().Length == 11)
                {
                    return isNumber(target);
                }
            }
            else if (target.StartsWith("0"))
            {
                if (target.Trim().Length == 10 || target.Trim().Length == 9 || target.Trim().Length == 11)
                {
                    return isNumber(target);
                }
            }
            return false;

        }

        public static int length(string text)
        {
            // return System.Text.Encoding.UTF8.GetByteCount(text);
            // return text.Length;
            return System.Text.Encoding.Default.GetBytes(text).Length;
        }

        /**
         * yy・mm・ddを受け取り、日付として正しいかどうかを判定します
         * @param yy 2000年からの経過年2桁
         * @param mm 月
         * @param dd 日
         * @return true:正しい/false:それ以外
         */
        public static bool isValidDate(string yy, string mm, string dd)
        {
            try
            {
                int yyi = int.Parse(yy);
                int mmi = int.Parse(mm);
                int ddi = int.Parse(dd);

                int year = 2000 + yyi;
                int days = DateTime.DaysInMonth(year, mmi);

                if (ddi >= 1 && ddi <= days && year >= 2000 && mmi >= 1 && mmi <= 12)
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public static string getBranchCodeNumberFormat(int code)
        {
            return string.Format("{0:000}", code);
        }

    }
        
    
}
